<template>
  <div>
    <div class="top">
      <span class="font">  筛选查询 </span>
      <el-button class="button" size="medium" @click="selectCust"
        >查询结果</el-button
      >
      <span class="dropdown" @click="drop">
        筛选查询<i class="el-icon-arrow-down"></i>
      </span>
    </div>
    <div class="dropmenu" :class="{ 'dropmenu-1': tab }">
      <span>输入搜索：</span>
      <input
        type="text"
        v-model="form.name"
        placeholder="客户名称／联系人／创建人／负责人"
      />
      <span>手机号：</span>
      <input type="text" v-model="form.cl_phone" placeholder="全部" />
      <span>创建时间</span>
      <el-date-picker
        v-model="value1"
        type="daterange"
        range-separator="-"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      >
      </el-date-picker>
    </div>
    <div>
      <div class="middle">
        <span class="font">  数据列表 </span>
        <!-- <el-select
          v-model="value2"
          multiple
          collapse-tags
          style="margin-left: 20px"
          placeholder="排序方式"
          class="select"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select> -->
        <el-select
          v-model="limit"
          placeholder="显示条数"
          class="select"
          @change="limitChange"
        >
          <el-option
            v-for="(item,index) in options"
            :key="index"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
        <!-- <el-button class="button" size="medium">地图模式</el-button> -->
        <el-button class="button" size="medium" @click="port">导出</el-button>
        <el-button class="button" size="medium" @click="Import">导入</el-button>
        <el-button class="button button-1" size="medium" @click="add"
          >添加</el-button
        >
      </div>
      <div>
        <el-table :data="tableData" border style="width: 100%" v-loading="loading">
          <el-table-column prop="cl_name" label="客户名称"> </el-table-column>
          <el-table-column prop="cl_type" label="客户类型"> </el-table-column>
          <el-table-column prop="cl_contactperson" label="联系人">
          </el-table-column>
          <el-table-column prop="cl_phone" label="手机号"> </el-table-column>
          <el-table-column prop="cl_address" label="地址"> </el-table-column>
          <el-table-column prop="cl_creatyperson" label="创建人">
          </el-table-column>
          <el-table-column prop="cl_Price_system" label="负责人">
          </el-table-column>
          <el-table-column prop="cl_createtime" label="创建时间">
          </el-table-column>
          <el-table-column prop="Operate" label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="sks(scope.row)"> 编辑 </el-button>
              <el-button type="text" @click="del(scope.row.cl_id)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="bottom clear">
          <el-pagination
            background
            layout="prev, pager, next, jumper"
            :total="total"
            @current-change="currPage"
            :current-page="page"
            :page-size="limit"
            style="float: right"
          >
          </el-pagination>
        </div>
      </div>
    </div>

    <el-dialog title="导入预览" :visible.sync="dialogTableVisible">
    <span style="font-size: 12px;color: #666666;">只显示前10条</span>
      <el-table :data="ImportData">
        <el-table-column prop="id" label="客户编号"> </el-table-column>
        <el-table-column prop="cl_name" label="客户名称"> </el-table-column>
        <el-table-column prop="cl_type" label="客户类型"> </el-table-column>
        <el-table-column prop="cl_contactperson" label="联系人">
        </el-table-column>
        <el-table-column prop="cl_phone" label="手机号"> </el-table-column>
        <el-table-column prop="cl_address" label="地址"> </el-table-column>
        <el-table-column prop="cl_creatyperson" label="创建人">
        </el-table-column>
        <el-table-column prop="cl_Price_system" label="负责人">
        </el-table-column>
        <el-table-column prop="cl_createtime" label="创建时间">
        </el-table-column>
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogTableVisible = false">取 消</el-button>
        <el-button type="primary" @click="addImport">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCustApi,
  delCustApi,
  InfoExportApi,
  SelectCustApi,
  ImportExcelApi,
} from "@/http/merchant/customerInfo";
import { fileExport, ImportExcel } from "@/util/export";
export default {
  data() {
    return {
      tab: false,
      loading:true,
      tableData: [],
      ImportData: [],
      form: {
        cl_phone: "",
        name: "",
        from: "",
        to: "",
      },
      options:[5,10,15],
      limit: 10,
      page: 1,
      total: 0,
      dialogTableVisible: false,
      importDataAll:[],
      arr: [
        "cl_name",
        "cl_type",
        "cl_contactperson",
        "cl_phone",
        "cl_address",
        "cl_creatyperson",
        "cl_leaderperson",
        "cl_longitude",
        "cl_latitude",
        "cl_Price_system",
        "cl_area",
        "cl_Landline_telephones",
        "cl_image",
        "cl_createtime",
      ],
    };
  },
  activated() {
    this.init(this.limit, this.page);
  },
  methods: {
    async init(limit, page) {
      let data = await getCustApi({ limit, page });
      console.log(data);
      if (data.data.code != 200) throw new Error();
      this.tableData = data.data.data;
      this.total = data.data.totalCount;
      this.loading=false
    },
    async del(id) {
      this.$confirm("确定删除？", "提示", {
        type: "error",
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(async () => {
        let data = await delCustApi({ id });
        if (data.data.code != 200) throw new Error();
        this.$message.success("删除成功");
        this.init(this.limit, this.page);
      });
    },
    async addCust() {},
    async port() {
      //导出
      try {
        let data = await InfoExportApi();
        console.log(data.data)
        fileExport(data.data, "导出");
      } catch (e) {
        console.log(e);
        this.$message.error("导出失败")
      }
    },
    Import() {
      //导入预览
      ImportExcel(this.arr, (data) => {
        this.importDataAll=data;
        if(data.length<=10){
           this.ImportData = data;
        }else{
          this.ImportData=data.slice(0,9);
        }
        this.$nextTick(() => {
          this.dialogTableVisible = true;
        });
      });
    },
    async addImport() {
      this.dialogTableVisible = false;
      let data = await ImportExcelApi(this.importDataAll);
      console.log(data)
    },
    async selectCust() {
      let data = await SelectCustApi(this.form);
      console.log(data);
      if (data.data.code != 200) throw new Error();
      this.tableData = data.data.data;
    },
    currPage(page) {
      this.init(this.limit, page);
      this.page=page
    },
    limitChange(limit){
      this.init(limit,this.page)
    },
    sks(row) {
      this.$router.push({ path: "/system-2/system-2-1-1",query:{row}});
    },
    add(){
       this.$router.push({ path: "/system-2/system-2-1-1"});
    },
    drop() {
      this.tab = !this.tab;
    },
  },
};
</script>

<style lang="scss" scoped>
.top {
  padding: 0 20px;
  box-sizing: border-box;
  width: 100%;
  height: 50px;
  background-color: #eee;
  border: 1px solid #ddd;
  border-bottom: none;

  & .font {
    line-height: 50px;
    float: left;
  }

  & .button {
    margin-top: 7px;
    float: right;
  }

  & .dropdown {
    line-height: 50px;
    float: right;
    margin: 0 50px;
    cursor: pointer;
  }
}

.dropmenu {
  padding: 0 20px 0;
  box-sizing: border-box;
  width: 100%;
  height: 50px;
  background-color: #fff;
  border: 1px solid #ddd;
  transition: all 0.5s;
  overflow: hidden;

  & span {
    margin: 4px 10px 0 100px;
  }

  & input {
    margin-top: 4px;
    width: 250px;
    height: 40px;
    border: 1px solid #eee;
  }
}

.dropmenu-1 {
  height: 0;
}

.middle {
  margin-top: 30px;
  padding: 0 20px 0;
  box-sizing: border-box;
  width: 100%;
  height: 50px;
  background-color: #eee;
  border: 1px solid #ddd;
  line-height: 50px;

  & .select {
    float: right;
    margin-right: 20px;
  }

  & .button {
    float: right;
    margin-right: 20px;
    margin-top: 4px;
  }
}

.bottom {
  padding: 4px 20px 0;
  box-sizing: border-box;
  width: 100%;
  height: 50px;
  background-color: #eee;
  border: 1px solid #ddd;
}
</style>